
//imports
import { homeData, getUpdate, wantDrink, hasDrink } from '../../../api/eapi';



/**
 * Product Page Config Object
 */
const pageConfig = {
  // let pageConfig = {
  /**
   * Data Object
   */
  data: {
    productData: null,
    homeData: '',
    icons: [{ img: '/images/drink_btn_gift_normal@2x.png', text: '我要送礼', ID: '1' },
    { img: '/images/drink_btn_dinner_normal@2x.png', text: '我有饭局', ID: '4' },
    { img: '/images/drink_btn_party_normal@2x.png', text: '我有聚会', ID: '2' },
    { img: '/images/drink_btn_daily_normal@2x.png', text: '日常口粮', ID: '3' }],
    nextPage: '',
    banner: '',
    pointing: ''
  },
  collect(e) {
    let homeData = this.data.homeData;
    let id = e.currentTarget.dataset.image;
    wx.showLoading({
      title: 'loading',
    });
    homeData.forEach(item => {
      if (item.id === id) {
        // 改变想喝的状态
        if (item.collect) {
          wantDrink.delete({ ids: id }).then(resp => {
            console.log(resp);
            item.collect = false;
            console.log(this.data.homeData);
            // 更新状态
            this.setData({ homeData: homeData }, () => wx.hideLoading());
          }).catch(err => console.error(err))
        } else {
          // 改变不想喝的状态
          wantDrink.post({ id: id }).then(resp => {
            console.log(resp);
            item.collect = true;
            console.log(this.data.homeData);
            // 更新状态
            this.setData({ homeData: homeData }, () => wx.hideLoading());
          }).catch(err => console.error(err))
        }
      }
    });
  },
  drink(e) {
    let homeData = this.data.homeData;
    let id = e.currentTarget.dataset.image;
    wx.showLoading({
      title: 'loading',
    });
    homeData.forEach(item => {
      if (item.id === id) {
        // 改变喝过的状态
        if (item.drink) {
          hasDrink.delete({ ids: id }).then(resp => {
            console.log(resp);
            item.drink = false;
            console.log(this.data.homeData);
            // 更新状态
            this.setData({ homeData: homeData }, () => wx.hideLoading());
          }).catch(err => console.error(err))
        } else {
          // 改变没喝过的状态
          hasDrink.post({ id: id }).then(resp => {
            console.log(resp);
            item.drink = true;
            console.log(this.data.homeData);
            // 更新状态
            this.setData({ homeData: homeData }, () => wx.hideLoading());
          }).catch(err => console.error(err))
        }
      }
    });
  },
  /**
   * 监听store state变化
   * 1. 设置动态详情标题
   * 2. 设置分享标题
   * 
   * @param nextState 下一个状态值
   */

  /**
   * 页面加载函数
   * 
   * @param options 传入参数对象
   */
  onLoad: function (options) {
    let _this = this;
    homeData("drink", {}).then(res => {

      let data = res.data.data.data;

      // 为文章添加已选择（想喝为collect、喝过为drink）的状态字段
      data.forEach(item => {
        item.collect = item.user_want_drink ? true : false;
        item.drink = item.user_has_drink ? true : false;
      })
      _this.setData({
        homeData: data,
        nextPage: res.data.data.next_page_url
      });

    });

    homeData('drink-focus', {}).then(res => {
      let data = res.data.data;

      _this.setData({
        pointing: data[0].id,
        banner: data
      })

    })
  },
  onShow: function () {
    let _this = this;
    wx.getStorage({
      key: 'drink',
      success: function (res) {
        if (res.data) {
          homeData("drink", {}).then(res => {
            let data = res.data.data.data;
            data.forEach(item => {
              item.collect = item.user_want_drink ? true : false;
              item.drink = item.user_has_drink ? true : false;
            })
            _this.setData({
              homeData: data,
              nextPage: res.data.data.next_page_url
            });

          });
          wx.setStorage({
            key: 'drink',
            data: false,
          })
        }
      },
    })
  },
  onReachBottom: function () {
    let _this = this;
    getUpdate(this.data.nextPage, _this);
  },
  onPullDownRefresh: function () {
    let _this = this;
    homeData("drink", {}).then(res => {
      let data = res.data.data.data;
      // 为文章添加已选择（已收藏）的状态字段
      data.forEach(item => {
        item.collect = item.user_want_drink ? true : false;
        item.drink = item.user_has_drink ? true : false;
      })
      _this.setData({
        homeData: data,
        nextPage: res.data.data.next_page_url
      });

      wx.showToast({
        title: '刷新成功',
        icon: "none",
        duration: 1000
      });
      wx.stopPullDownRefresh()
    });

    homeData('drink-focus', {}).then(res => {
      let data = res.data.data;

      _this.setData({
        pointing: data[0].id,
        banner: data
      })

    })
  },
  pointing(e) {

    this.setData({
      pointing: e.detail.currentItemId
    })
  }
}


/**
 * Page页面
 * 
 * Redux connect state and action to page
 */
Page(pageConfig);
